<?php
defined('_JEXEC') or die('Restricted access');
require_once(JPATH_COMPONENT.DS.'data'.DS.'DAORouteComment.php');
require_once(JPATH_COMPONENT.DS.'dominio'.DS.'DTOFactory.php');

class JDAORouteComment implements DAORouteComment{
	
	public function __construct(){
		
	}
	
	private function multiObjectQuery($query){
		$db =& JFactory::getDBO();
		$db->setQuery( $query );
		$rows = $db->loadObjectList();
		if ($db->getErrorNum()) {
			echo $db->stderr();
			return false;
		}
		return $rows;
	}	
	
	public function getRouteComments($id){
		$rows=$this->multiObjectQuery("SELECT #__sr_routecomments.id,routeID,userID,username,commentDate,commentText
						 			   FROM #__sr_routecomments 
						 			   INNER JOIN #__users ON userID=#__users.id 
						 			   WHERE routeID = '".$id."'
						 			   ORDER BY commentDate DESC");
		return DTOFactory::getInstance()->getDTOComments($rows);
	}
	
	public function saveComment(){
		$row =& JTable::getInstance('routecomments', 'Table');
		
		if (!$row->bind(JRequest::get('post')))
		{
			return false;
		}
		$row->commentDate = date( 'Y-m-d H:i:s' );
		$row->userID = JFactory::getUser()->get('id');
		if (!$row->store())
		{
			return false;
		}
		return true;
	}
	
}

?>